import { Collection, EntitySchema, Ref } from '@mikro-orm/core';
import { JobTasks } from './JobTasks';
import { Users } from './Users';

export class JobProjectSources {
  id!: string;
  createdAt?: Date;
  updatedAt?: Date;
  creator?: Ref<Users>;
  owner?: Ref<Users>;
  title!: string;
  tasks = new Collection<JobTasks>(this);
}

export const JobProjectSourcesSchema = new EntitySchema({
  class: JobProjectSources,
  properties: {
    id: { primary: true, type: 'string', columnType: 'bigint' },
    createdAt: { type: 'Date', length: 0, nullable: true },
    updatedAt: { type: 'Date', length: 0, nullable: true },
    creator: {
      reference: 'm:1',
      entity: () => Users,
      ref: true,
      nullable: true,
      index: 'creator_foreign_key',
    },
    owner: {
      reference: 'm:1',
      entity: () => Users,
      ref: true,
      nullable: true,
      index: 'owner_foreign_key',
    },
    title: { type: 'string', length: 255 },
    tasks: { reference: '1:m', entity: () => JobTasks, mappedBy: 'source' },
  },
});
